/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.server.impl;


import com.by.common.utils.AutoCode;
import com.by.common.utils.AutoData;
import com.by.mapper.PurInquiryDetailsMapper;
import com.by.mapper.PurInquiryMapper;
import com.by.mapper.PurInquiryProMapper;
import com.by.pojo.PurInquiry;
import com.by.server.PurInquiryServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Max;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>Project: erp-cloud-api - PurInquiryServerImpl</p>
 * <p>Powered by yzc On 2024-01-10 15:23:37</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class PurInquiryServerImpl implements PurInquiryServer {

    @Autowired
    PurInquiryMapper purInquiryMapper;

    @Autowired
    PurInquiryDetailsMapper purInquiryDetailsMapper;

    @Autowired
    PurInquiryProMapper purInquiryProMapper;

    @Override
    @Transactional
    public int save(PurInquiry purInquiry) {
     //根据来源获得Code前缀
     String code =  AutoCode.autoCodePrefix("询价")+ AutoData.getNowData();
     //根据Code前缀查询最大的Code
     String maxCode =  purInquiryMapper.MaxCode(code);
     //获得新的Code
     String newCode =AutoCode.getNewCode(maxCode,code);

     purInquiry.setCode(newCode);

     //设置关联字段
     purInquiry.getPurInquiryPro().setInquiryCode(newCode);
     //保存询价产品
     purInquiryProMapper.insert(purInquiry.getPurInquiryPro());
     //遍历询价详情list集合
     purInquiry.getPurInquiryDetailsList().forEach(pd->{
         //设置关联字段
         pd.setInquiryCode(newCode);
         //保存询价详情
         purInquiryDetailsMapper.insert(pd);
     });

        return purInquiryMapper.insert(purInquiry);
    }



}
